package com.fr.cell;

import com.fr.report.FloatElement;
import com.fr.report.Report;
import com.fr.report.TemplateReport;
import com.fr.util.KeyEventWork;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JScrollBar;

public class GridKeyListener
  implements KeyListener
{
  private Grid grid;
  private long keyPressedLastTime = 0L;
  private boolean isKeyPressedContentChanged = false;

  public GridKeyListener(Grid paramGrid)
  {
    this.grid = paramGrid;
  }

  public void keyPressed(KeyEvent paramKeyEvent)
  {
    if ((!(this.grid.isEnabled())) || (!(this.grid.isCellSelectable())))
      return;
    if (paramKeyEvent.isConsumed())
      return;
    KeyEvent localKeyEvent = KeyEventWork.processKeyEvent(paramKeyEvent);
    if (localKeyEvent == null)
      return;
    long l = System.currentTimeMillis();
    int i = paramKeyEvent.getKeyCode();
    int j = 0;
    ReportPane localReportPane = this.grid.getReportPane();
    TemplateReport localTemplateReport = localReportPane.getEditingReport();
    localTemplateReport.getColumnCount();
    localTemplateReport.getRowCount();
    GridSelection localGridSelection = localReportPane.getGridSelection();
    localReportPane.getGridRow();
    int k = localGridSelection.getType();
    if (k == 1)
    {
      if (l - this.keyPressedLastTime <= 2L)
        return;
      this.keyPressedLastTime = l;
      int i1 = 0;
      if (!(this.isKeyPressedContentChanged))
        this.isKeyPressedContentChanged = true;
      FloatElement localFloatElement = localTemplateReport.getFloatElement(localGridSelection.getFloatName(0));
      switch (i)
      {
      case 37:
        if (localFloatElement.getLeftDistance() <= 0)
        {
          localFloatElement.setColumn(localFloatElement.getColumn() - 1);
          localFloatElement.setLeftDistance(localTemplateReport.getColumnWidth(localFloatElement.getColumn()));
        }
        else
        {
          localFloatElement.setLeftDistance(localFloatElement.getLeftDistance() - 1);
        }
        i1 = 1;
        break;
      case 39:
        if (localFloatElement.getLeftDistance() >= localTemplateReport.getColumnWidth(localFloatElement.getColumn()))
        {
          localFloatElement.setColumn(localFloatElement.getColumn() + 1);
          localFloatElement.setLeftDistance(0);
        }
        else
        {
          localFloatElement.setLeftDistance(localFloatElement.getLeftDistance() + 1);
        }
        i1 = 1;
        break;
      case 38:
        if (localFloatElement.getTopDistance() <= 0)
        {
          localFloatElement.setRow(localFloatElement.getRow() - 1);
          localFloatElement.setTopDistance(localTemplateReport.getRowHeight(localFloatElement.getRow()));
        }
        else
        {
          localFloatElement.setTopDistance(localFloatElement.getTopDistance() - 1);
        }
        i1 = 1;
        break;
      case 40:
        if (localFloatElement.getTopDistance() >= localTemplateReport.getRowHeight(localFloatElement.getRow()))
        {
          localFloatElement.setRow(localFloatElement.getRow() + 1);
          localFloatElement.setTopDistance(0);
        }
        else
        {
          localFloatElement.setTopDistance(localFloatElement.getTopDistance() + 1);
        }
        i1 = 1;
      }
      if (i1 != 0)
        this.grid.getReportPane().repaint();
    }
    else
    {
      if (l - this.keyPressedLastTime <= 32L)
        return;
      this.keyPressedLastTime = l;
      switch (i)
      {
      case 27:
        if (this.grid.isCellEditing())
          this.grid.cancelEditing();
        break;
      case 113:
        if (!(this.grid.isCellEditing()))
          this.grid.startEditing();
      }
      if ((i == 96) || (i == 97) || (i == 98) || (i == 99) || (i == 100) || (i == 101) || (i == 102) || (i == 103) || (i == 104) || (i == 105) || (i == 106) || (i == 107) || (i == 109) || (i == 110) || (i == 111))
        keyTyped(paramKeyEvent);
    }
    switch (i)
    {
    case 33:
      localReportPane.getVerticalScrollBar().setValue(Math.max(0, this.grid.getVerticalValue() - this.grid.getVerticalExtent()));
      j = 1;
      break;
    case 34:
      localReportPane.getVerticalScrollBar().setValue(this.grid.getVerticalValue() + this.grid.getVerticalExtent());
      j = 1;
      break;
    case 65:
      if ((i == 65) && (paramKeyEvent.isControlDown()))
      {
        localGridSelection.clearAllCellRectangles();
        localGridSelection.addCellRectangle(new Rectangle(0, 0, localTemplateReport.getColumnCount(), localTemplateReport.getRowCount()));
        j = 1;
      }
      j = 1;
    }
    if (j != 0)
      localReportPane.repaint();
  }

  public void keyReleased(KeyEvent paramKeyEvent)
  {
    if ((!(this.grid.isEnabled())) || (!(this.grid.isCellSelectable())))
      return;
    if (paramKeyEvent.isConsumed())
      return;
    KeyEvent localKeyEvent = KeyEventWork.processKeyEvent(paramKeyEvent);
    if (localKeyEvent == null)
      return;
    if (this.isKeyPressedContentChanged)
    {
      this.grid.getReportPane().fireReportDataChanged();
      this.grid.getReportPane().undoRecord();
      this.isKeyPressedContentChanged = false;
    }
  }

  public void keyTyped(KeyEvent paramKeyEvent)
  {
    if ((!(this.grid.isEnabled())) || (!(this.grid.isCellSelectable())))
      return;
    if (paramKeyEvent.isConsumed())
      return;
    KeyEvent localKeyEvent1 = KeyEventWork.processKeyEvent(paramKeyEvent);
    if (localKeyEvent1 == null)
      return;
    if (paramKeyEvent.isControlDown())
      return;
    int i = paramKeyEvent.getKeyCode();
    char c = paramKeyEvent.getKeyChar();
    if (c == '\t')
      return;
    if (Character.isDefined(c))
    {
      GridSelection localGridSelection = this.grid.getReportPane().getGridSelection();
      int j = localGridSelection.getType();
      if (j == 0)
      {
        if (!(this.grid.isCellEditing()))
          this.grid.startEditing(paramKeyEvent);
        if ((this.grid.getCellEditor() != null) && (this.grid.editorComponent != null))
          if ((i == 96) || (i == 97) || (i == 98) || (i == 99) || (i == 100) || (i == 101) || (i == 102) || (i == 103) || (i == 104) || (i == 105) || (i == 110) || (i == 106) || (i == 107) || (i == 109) || (i == 111) || (i == 127))
          {
            KeyEvent localKeyEvent2 = new KeyEvent(this.grid, 401, 0L, 0, i - 48, c);
            this.grid.editorComponent.dispatchEvent(localKeyEvent2);
            localKeyEvent2.consume();
          }
          else if (!(paramKeyEvent.isConsumed()))
          {
            this.grid.editorComponent.dispatchEvent(paramKeyEvent);
          }
      }
    }
  }
}